package com.xjm_cloud_admin.config.security.handle;

import com.alibaba.fastjson2.JSON;
import com.xjm_cloud_admin.enums.ResultCodeEnum;
import com.xjm_cloud_admin.util.R;
import com.xjm_cloud_admin.util.ServletUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 当在认证阶段抛出异常时该处理器会被调用
 * 自定义返回结果：没有登录或token过期
 *
 * @author 浪漫写尽歌谱
 */
@Component
public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) {
        ServletUtils.renderString(httpServletResponse, JSON.toJSONString(R.error(ResultCodeEnum.LOGIN_INVALID.getCode(), ResultCodeEnum.LOGIN_INVALID.getMessage())));
    }

}
